<?php

/**
 * @功能 将表单插入数据库
 */
$link = require_once ('./config.php');
require_once ('./function.php');
mysqli_query($link, 'set names utf8');
//获取ip
if ($_SERVER['HTTP_X_REAL_IP']) {//nginx 代理模式下，获取客户端真实IP
    $ip = $_SERVER['HTTP_X_REAL_IP'];
} elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {//客户端的ip
    $ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {//浏览当前页面的用户计算机的网关
    $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
    $pos = array_search('unknown', $arr);
    if (false !== $pos)
        unset($arr[$pos]);
    $ip = trim($arr[0]);
}elseif (isset($_SERVER['REMOTE_ADDR'])) {
    $ip = $_SERVER['REMOTE_ADDR']; //浏览当前页面的用户计算机的ip地址
} else {
    $ip = $_SERVER['REMOTE_ADDR'];
}
//颜色组
$color = array('success', 'info', 'warning', 'danger');
if (!isset($_COOKIE['color'])) {
    $colorno = rand(0, 3);
//一天一换颜色
    setcookie("color", $color[$colorno], time() + 3600 * 24);
    $selfcolor = $color[$colorno];
} else
    $selfcolor = $_COOKIE['color'];
if (empty($_COOKIE['ip']))
    setcookie("ip", $ip, time() + 3600 * 24 * 360);
if (!empty($content = $_POST['content'])) {
    $time = date('Y-m-d H:i:s', time(0));
    $content = htmlentities($content); //对html特殊字符编码
    $selfcolor = empty($selfcolor) ? 'success' : $selfcolor;
    $location = position($ip);
    if ($_GET['talk'] == "selftalk") {
        $table = "selftalk";
        $replymsg = replay($content, $ip);
    } else {
        $table = "talk";
    }

    $sql = "INSERT INTO " . $table . " VALUES(NULL,'匿名者','$ip','$content','$time','$selfcolor','$location')";
    $data = mysqli_query($link, $sql);
    if ($_GET['talk'] == "selftalk") {
        $sql = "INSERT INTO " . $table . " VALUES(NULL,'小木','$ip','$replymsg','$time','success','中国湖北武汉')";
        $data = mysqli_query($link, $sql);
    }
}
